/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RatingPanel.java
 *
 * Created on Aug 27, 2009, 7:42:06 AM
 */

package matpred;

import bdl.UtilityBean;
import entities.GCalendar;
import entities.Match;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;
import rating.SelectDialog;
import utilities.BusyDialog;
import utilities.Rating;
import utilities.Utility;
import utilities.entities.SCalendar;
import utilities.entities.SelectionList;
import utilities.entities.Selection;

/**
 *
 * @author Demola
 */
public class RatingPanel extends javax.swing.JPanel {

    /** Creates new form RatingPanel */
    public RatingPanel() {
        initComponents();
        loadDates();
        load();
    }

    public static RatingPanel getRatingPanel(){
        if(rp == null)
            rp = new RatingPanel();
        return rp;
    }

    public void addSelection(ArrayList<Selection> selections, boolean only){
        ratingResult = new SelectionList(selections, only);
    }

    private void loadDates(){
        SCalendar dCal = CalendarPanel.getCalendarPanel().getDefaultCalendar();
        if(dCal != null){
            DefaultTableModel tm = dCal.getTable();
            jComboBox1.removeAllItems();
            jComboBox2.removeAllItems();
            for(int i = 0; i < tm.getRowCount(); i++){
                jComboBox1.addItem(tm.getValueAt(i, 1));
                jComboBox2.addItem(tm.getValueAt(i, 1));
            }
            jComboBox1.setSelectedIndex(0);
            jComboBox2.setSelectedIndex(0);
        }
    }

    private void load(){
        List<Match> mlist = UtilityBean.getPMatches();
        HashSet<Match> set = new HashSet();
        if(mlist != null || !mlist.isEmpty()){
            for(Match m : mlist){
                if(set.add(m)){
                    jComboBox3.addItem(sdf.format(m.getMatchDate()));
                    jComboBox4.addItem(sdf.format(m.getMatchDate()));
                }
            }
        }
    }

    

    //isSelectionUnique
//    private boolean isSelectionUnique(SelectionList rating){
//        if(ratingList.isEmpty()) return true;
//        ArrayList<Selection> newSelecteds = rating.getSelected();
//        outer:
//            for(SelectionList res : ratingList){
//            ArrayList<Selection> oldSelecteds = res.getSelected();
//            if(rating.isOnly() != res.isOnly()){
//                continue;
//            }
//            else if(oldSelecteds.isEmpty() && newSelecteds.isEmpty()){
//                return false;
//            }
//            else if(oldSelecteds.size() != newSelecteds.size()){
//                continue;
//            }
//            else{
//                for(int i = 0; i < newSelecteds.size(); i++){
//                    int nId = newSelecteds.get(i).getColumn().getPosition();
//                    int oId = oldSelecteds.get(i).getColumn().getPosition();
//                    if(nId != oId)
//                        continue outer;
//                }
//                return false;
//            }
//        }
//        return true;
//    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jComboBox4 = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jComboBox3 = new javax.swing.JComboBox();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Auto Rating"));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Start Date");

        jLabel2.setText("End Date");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Total Search Matching Result Column");

        jLabel4.setText("to");

        jLabel5.setText("Mininmum Success Count");

        jButton5.setText("Auto Search");
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton5MouseClicked(evt);
            }
        });

        jLabel7.setText("Mininmum Failure Count");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addGap(18, 18, 18)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton5))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(16, 16, 16)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel2)
                            .addGap(16, 16, 16)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jButton5)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Prediction Rating"));

        jLabel6.setText("Match Play Date");

        jButton6.setText("Probability Rating");
        jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton6MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(jButton6)
                .addGap(66, 66, 66))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Rate"));

        jButton1.setText("Extract");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Select");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("Rate");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(jButton3)
                .addGap(47, 47, 47)
                .addComponent(jButton1)
                .addContainerGap(36, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(211, 211, 211))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(63, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
         new SelectDialog(null, true).setVisible(true);
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        if(ratingResult == null)
            JOptionPane.showMessageDialog(null, "Make selections first");
        else{
            final BusyDialog busy = new BusyDialog(null, true);
            String sdate = (String) jComboBox3.getSelectedItem();
            Date date = Utility.parseDate(sdate);
            final Rating rating = new Rating(date);
            SwingWorker<Boolean, Void> sw = new SwingWorker() {
                @Override
                protected Boolean doInBackground() throws Exception {
                    rating.rate(ratingResult);
                    return rating.isValid();
                }

                @Override
                protected void done(){
                    System.out.println("finished");
                    busy.setVisible(false);
                }
            };
            sw.execute();
            busy.setVisible(true);
            if(busy.isCancel()){
                sw.cancel(true);
                JOptionPane.showMessageDialog(null, "Search cancelled");
                return;
            }
            try {
                if (sw.get()) {
                    new Display(rating.getTableModel()).setVisible(true);
                    GCalendar tcal = UtilityBean.getCalendar();
                    if (tcal != null && tcal.getNYears() != -1) {
                        int ofset = tcal.getOffset() + 1;
                        tcal.setOffset(ofset);
                        UtilityBean.updateCalendar(tcal);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, rating.getMessage());
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "No result found E");
            }

        }
    }//GEN-LAST:event_jButton3MouseClicked

    private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton5MouseClicked
        Date sdate = Utility.parseDate(jComboBox1.getSelectedItem().toString());
        Date edate = Utility.parseDate(jComboBox2.getSelectedItem().toString());
        int a = Utility.parseInt(jTextField1.getText());
        int b = Utility.parseInt(jTextField2.getText());
        final int minFt = Utility.parseInt(jTextField3.getText());
        final int [] range = new int [] {a, b};
        if(a > -1 && b > -1){
            range[0] = a;
            range[1] = b;
        }
        final Rating rating = new Rating(sdate, edate);
        final BusyDialog busy = new BusyDialog(null, true);
        SwingWorker<Boolean, Void> sw = new SwingWorker() {
            @Override
            protected Boolean doInBackground() throws Exception {
                rating.autosearch(range, minFt);
                return rating.isValid();
            }

            @Override
            protected void done(){
                System.out.println("finished");
                busy.setVisible(false);
            }
        };
        sw.execute();
        busy.setVisible(true);
        if(busy.isCancel()){
            sw.cancel(true);
            JOptionPane.showMessageDialog(null, "Search cancelled");
            return;
        }
        try {
            if (sw.get()) {
                new Display(rating.getTableModel()).setVisible(true);
            } else {
                JOptionPane.showMessageDialog(null, rating.getMessage());
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "No result found E");
        }
    }//GEN-LAST:event_jButton5MouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
         if(ratingResult == null)
            JOptionPane.showMessageDialog(null, "Make selections first");
        else{
             final BusyDialog busy = new BusyDialog(null, true);
            String sdate = (String) jComboBox3.getSelectedItem();
            Date date = Utility.parseDate(sdate);
            final Rating rating = new Rating(date);
            SwingWorker<Boolean, Void> sw = new SwingWorker() {
                @Override
                protected Boolean doInBackground() throws Exception {
                    rating.extract(ratingResult);
                    return rating.isValid();
                }

                @Override
                protected void done(){
                    System.out.println("finished");
                    busy.setVisible(false);
                }
            };
            sw.execute();
            busy.setVisible(true);
            if(busy.isCancel()){
                sw.cancel(true);
                JOptionPane.showMessageDialog(null, "Search cancelled");
                return;
            }
            try {
                if (sw.get()) {
                    new Display(rating.getTableModel()).setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, rating.getMessage());
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "No result found E");
            }

        }
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton6MouseClicked
         String sdate = (String) jComboBox4.getSelectedItem();
         Date date = Utility.parseDate(sdate);
         final Rating rating = new Rating(date);
         final BusyDialog busy = new BusyDialog(null, true);
         SwingWorker<Boolean, Void> sw = new SwingWorker() {
            @Override
            protected Boolean doInBackground() throws Exception {
                rating.probabiltyRate();
                return rating.isValid();
            }

            @Override
            protected void done(){
                System.out.println("finished");
                busy.setVisible(false);
            }
        };
        sw.execute();
        busy.setVisible(true);
        if(busy.isCancel()){
            sw.cancel(true);
            JOptionPane.showMessageDialog(null, "Search cancelled");
            return;
        }
        try {
            if (sw.get()) {
                new Display(rating.getTableModel()).setVisible(true);
            } else {
                JOptionPane.showMessageDialog(null, rating.getMessage());
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "No result found E");
        }
    }//GEN-LAST:event_jButton6MouseClicked


    private static SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy");
    private SelectionList ratingResult;
    private static RatingPanel rp;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration//GEN-END:variables

}
